#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <fcntl.h>

#include "mapmod.h"

#define	DEVICE	"/dev/memio0"

int main ( int argc, char* argv[] ) {
	int i = 0;
	long sz = sysconf(_SC_PAGESIZE);
	void* mem = NULL;
	struct UserPages uPages = { .ptr = NULL, .nr_pages = 0 };
	ssize_t rv = 0;
	int dd = open( DEVICE, O_RDONLY );
	if ( dd == -1 ) {
		perror( "Can't open file " DEVICE );
		return -1;
	}
	mem = aligned_alloc( sz, 7*sz );
	if (!mem) {
		perror( "Can't allocate aligned memory\n" );
		return -1;
	} else {
		uPages.ptr = mem;
		uPages.nr_pages = 7;
		for ( i = 0; i < 7*sz; ++i ) {
			*((char*)(mem+i)) = 65 /*+ i%26*/;
		}
	}
	printf( "Allocated memory is %p\n", mem );

	ioctl( dd, MAPMOD_IOCTL_SET_BUFFER, &uPages );
	free( mem );
	close( dd );
	return 0;
}
